package com.haiyi.software.app.basic;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpResponse;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.app.R;
import com.haiyi.software.entity.BeanLoginResultBody;
import com.haiyi.software.entity.UserInfo;
import com.haiyi.software.tools.L;
import com.haiyi.software.tools.MyTools;
import com.haiyi.software.ui.controller.WatingProgressDialog;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.MyAsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;

public abstract class ActivityMineBasicUpload extends ActivityMineBasic {

	private final String str_requestNameUpload = "upload.php";

	protected abstract void UploadImageSuccess();

	protected JsonObject jsonReturn = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
	}

	protected void UploadImage(String imageSrc, int type) {

		File uploadFileImage = new File(imageSrc.trim());

		if (uploadFileImage.exists() && uploadFileImage.length() > 0) {

			UserInfo myInfo = MainApplication.getInstance().getMineUserInfo();

			if (myInfo != null) {

				Map<String, Object> validator = new HashMap<String, Object>();

				validator.put("uid", myInfo.getUid());
				validator.put("uname", myInfo.getUname());
				validator.put("pwd", myInfo.getPwd());
				validator.put("uguid", myInfo.getUguid());

				Map<String, Object> body = new HashMap<String, Object>();
				body.put("typeid", type);

				body.put("filedatasrc", uploadFileImage.getPath());

				body.put("videolong", 0);

				body.put("md5id", MyTools.getMd5ByFile(uploadFileImage));

				Map<String, Object> args = new HashMap<String, Object>();
				args.put("validator", validator);
				args.put("body", body);

				Gson gson = new Gson();

				String s = gson.toJson(args);

				upLoadImageFile(imageSrc, s, uploadFileImage);
			}
		}
	}

	private void upLoadImageFile(String imageSrc, String args, File file) {

		final AsyncHttpClient client = MainApplication.getInstance()
				.getAsyncHttpClient();

		watingDialog = new WatingProgressDialog(this,
				R.style.WatingProgressDialog);

		watingDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				// TODO 自动生成的方法存根
				if (client != null)
					client.cancelRequests(ActivityMineBasicUpload.this, true);
			}
		});

		watingDialog.show();

		String contType = URLConnection.guessContentTypeFromName(imageSrc);

		RequestParams params = new RequestParams();

		try {
			params.put("args", args);

			params.put("attfile", file, contType);

			client.setTimeout(60000);

			client.post(str_server + str_requestNameUpload, params, rpHandler);

		} catch (FileNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	private ResponseHandlerInterface rpHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int statusCode, Header[] headers,
				byte[] responseBody) {

			watingDialog.hide();

			try {
				String str_jsonReturn = new String(responseBody, "UTF-8");

				L.i("return=" + str_jsonReturn);

				JsonParser parse = new JsonParser();

				jsonReturn = parse.parse(str_jsonReturn).getAsJsonObject();

				UploadImageSuccess();

			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}

		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] responseBody, Throwable error) {

			watingDialog.hide();

			Toast.makeText(ActivityMineBasicUpload.this, "上传失败", 1).show();
		}

	};

	// private String getJsonArgs() {
	//
	// UserInfo myInfo = MainApplication.getInstance().getMineUserInfo();
	//
	// if (myInfo != null) {
	//
	// Map<String, Object> validator = new HashMap<String, Object>();
	//
	// validator.put("uid", myInfo.getUid());
	// validator.put("uname", myInfo.getUname());
	// validator.put("pwd", myInfo.getPwd());
	// validator.put("uguid", myInfo.getUguid());
	//
	// Map<String, Object> body = new HashMap<String, Object>();
	// body.put("typeid", 1);
	// body.put("filedatasrc", uploadFileImage.getPath());
	//
	// body.put("videolong", 0);
	//
	// body.put("md5id", MyTools.getMd5ByFile(uploadFileImage));
	//
	// Map<String, Object> args = new HashMap<String, Object>();
	// args.put("validator", validator);
	// args.put("body", body);
	//
	// Gson gson = new Gson();
	// String s = gson.toJson(args);
	//
	// Log.i("argsJson", s);
	// return s;
	// } else {
	// return "";
	// }
	// }
}
